package com.weareint.feigncopy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

import java.util.concurrent.TimeUnit;

/**
 *
 *
 * <pre>
 *
 * </pre>
 *
 * @author johnny
 * @date 2021-07-25 4:21 下午
 */
@Slf4j
public class FeignCopyUtils {

    /**
     * 映射 Long 类型 主键
     *
     * @param cacheParseFeignDataService : AbsRedisCacheMappingFeignDataService
     * @param timeUnit : 缓存时长单位
     * @param duration : 缓存时长
     * @param <S> : D
     * @param <D> : D
     */
    public static <S, D> void mapping(
            AbsRedisCacheMappingFeignDataService<S, D, Long> cacheParseFeignDataService,
            TimeUnit timeUnit,
            long duration) {
        StopWatch stopWatch = null;
        if (log.isDebugEnabled()) {
            stopWatch = new StopWatch();
            stopWatch.start();
            log.debug(
                    "【-------------feign mapping {} start-------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
        cacheParseFeignDataService.mapping(timeUnit, duration);
        if (log.isDebugEnabled()) {
            assert stopWatch != null;
            stopWatch.stop();
            log.debug("【feign mapping duration : {} ms】", stopWatch.getTotalTimeMillis());
            log.debug(
                    "【-------------feign mapping {} end  -------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
    }

    /**
     * 映射其他类型 T 主键
     *
     * @param cacheParseFeignDataService : AbsRedisCacheMappingFeignDataService
     * @param timeUnit : 缓存时长单位
     * @param duration : 缓存时长
     * @param <S> : D
     * @param <D> : D
     * @param <T> : 主键类型
     */
    public static <S, D, T> void otherTypeMapping(
            AbsRedisCacheMappingFeignDataService<S, D, T> cacheParseFeignDataService,
            TimeUnit timeUnit,
            long duration) {
        StopWatch stopWatch = null;
        if (log.isDebugEnabled()) {
            stopWatch = new StopWatch();
            stopWatch.start();
            log.debug(
                    "【-------------feign mapping {} start-------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
        cacheParseFeignDataService.mapping(timeUnit, duration);
        if (log.isDebugEnabled()) {
            assert stopWatch != null;
            stopWatch.stop();
            log.debug("【feign mapping duration : {} ms】", stopWatch.getTotalTimeMillis());
            log.debug(
                    "【-------------feign mapping {} end  -------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
    }
    /**
     * 映射 Long 类型 主键
     *
     * @param cacheParseFeignDataService : AbsCaffeineCacheMappingFeignDataService
     * @param <S> : D
     * @param <D> : D
     */
    public static <S, D> void mapping(
            AbsCaffeineCacheMappingFeignDataService<S, D, Long> cacheParseFeignDataService) {
        StopWatch stopWatch = null;
        if (log.isDebugEnabled()) {
            stopWatch = new StopWatch();
            stopWatch.start();
            log.debug(
                    "【-------------feign mapping {} start-------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
        cacheParseFeignDataService.mapping(null, 0);
        if (log.isDebugEnabled()) {
            assert stopWatch != null;
            stopWatch.stop();
            log.debug("【feign mapping duration : {} ms】", stopWatch.getTotalTimeMillis());
            log.debug(
                    "【-------------feign mapping {} end  -------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
    }

    /**
     * 映射其他类型 T 主键
     *
     * @param cacheParseFeignDataService : AbsCaffeineCacheMappingFeignDataService
     * @param <S> : D
     * @param <D> : D
     * @param <T> : 主键类型
     */
    public static <S, D, T> void otherTypeMapping(
            AbsCaffeineCacheMappingFeignDataService<S, D, T> cacheParseFeignDataService) {
        StopWatch stopWatch = null;
        if (log.isDebugEnabled()) {
            stopWatch = new StopWatch();
            stopWatch.start();
            log.debug(
                    "【-------------feign mapping {} start-------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
        cacheParseFeignDataService.mapping(null, 0);

        if (log.isDebugEnabled()) {
            assert stopWatch != null;
            stopWatch.stop();
            log.debug("【feign mapping duration : {} ms】", stopWatch.getTotalTimeMillis());
            log.debug(
                    "【-------------feign mapping {} end  -------------】",
                    cacheParseFeignDataService.context().keyPrefix());
        }
    }

    public static <S, D> void otherTypeNoCacheMapping(
            MappingFeignDataService<S, D, Long> mappingFeignDataService) {
        StopWatch stopWatch = null;
        if (log.isDebugEnabled()) {
            stopWatch = new StopWatch();
            stopWatch.start();
            log.debug("【-------------feign mapping start-------------】");
        }
        mappingFeignDataService.mapping();
        if (log.isDebugEnabled()) {
            assert stopWatch != null;
            stopWatch.stop();
            log.debug("【feign mapping duration : {} ms】", stopWatch.getTotalTimeMillis());
        }
    }

    public static <S, D> void noCacheMapping(
            NoCacheMappingFeignDataService<S, D, Long> mappingFeignDataService) {
        StopWatch stopWatch = null;
        if (log.isDebugEnabled()) {
            stopWatch = new StopWatch();
            stopWatch.start();
        }

        mappingFeignDataService.mapping();
        if (log.isDebugEnabled()) {
            assert stopWatch != null;
            stopWatch.stop();
            log.debug("【feign mapping duration : {} ms】", stopWatch.getTotalTimeMillis());
        }
    }
}
